/**
 * @Title: HttpClientUtil.java
 * @Package com.themis.common.http
 * @Description: 网络访问工具
 * @author lxr
 * @date 2018年3月23日 下午7:37:43
 * @version V1.0
 */
package com.thinkgem.jeesite.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.*;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

/**
 * @ClassName: HttpClientUtil
 * @Description: HttpClient访问远程接口
 * @author lxr
 * @date 2018年3月23日 下午7:37:43
 */
@SuppressWarnings("deprecation")
public class HttpClientUtil {

    private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

    public static String httpRequestToString(String url, String requestMethod, Map<String, Object> params,
                                             String... auth) {

        // 接口返回结果
        String methodResult = null;
        try {

            String parameters = "";
            boolean hasParams = false;
            // // 将参数集合拼接成特定格式，如name=zhangsan&age=24
            if (!params.isEmpty()) {
                for (String key : params.keySet()) {
                    String value = URLEncoder.encode(params.get(key) == null ? "" : params.get(key).toString(), "UTF-8");
                    parameters += key + "=" + value + "&";
                    hasParams = true;
                }
                if (hasParams) {
                    parameters = parameters.substring(0, parameters.length() - 1);
                }
            }

            // 是否为GET方式请求
            boolean isGet = "get".equalsIgnoreCase(requestMethod);
            boolean isPost = "post".equalsIgnoreCase(requestMethod);
            boolean isPut = "put".equalsIgnoreCase(requestMethod);
            boolean isDelete = "delete".equalsIgnoreCase(requestMethod);

            // 创建HttpClient连接对象
            DefaultHttpClient client = new DefaultHttpClient();
            HttpRequestBase method = null;
            if (isGet) {
                url += "?" + parameters;
                method = new HttpGet(url);
            } else if (isPost) {
                method = new HttpPost(url);
                HttpPost postMethod = (HttpPost) method;
                method.addHeader("Content-Type", "application/json; charset=UTF-8");
                StringEntity entity = new StringEntity(JSONObject.parseObject(JSON.toJSONString(params)).toString(),
                        "UTF-8");
                entity.setContentEncoding("UTF-8");
                // 发送Json格式的数据请求
                entity.setContentType("application/json");
                postMethod.setEntity(entity);
            } else if (isPut) {
                method = new HttpPut(url);
                HttpPut putMethod = (HttpPut) method;
                StringEntity entity = new StringEntity(parameters.toString());
                putMethod.setEntity(entity);
            } else if (isDelete) {
                url += "?" + parameters;

                method = new HttpDelete(url);
            }
            method.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 2000);
            // 设置参数内容类型
            method.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            // httpClient本地上下文
            HttpClientContext context = null;

            if (!(auth == null || auth.length == 0)) {
                String username = auth[0];
                String password = auth[1];
                UsernamePasswordCredentials credt = new UsernamePasswordCredentials(username, password);
                // 凭据提供器
                CredentialsProvider provider = new BasicCredentialsProvider();
                // 凭据的匹配范围
                provider.setCredentials(AuthScope.ANY, credt);
                context = HttpClientContext.create();
                context.setCredentialsProvider(provider);
            }

            // 访问接口，返回状态码
            // HttpResponse response = (HttpResponse) client.execute(method, context);
            HttpResponse response = client.execute(method);

            // 返回状态码200，则访问接口成功
            if (response.getStatusLine().getStatusCode() == 200) {
                // methodResult = EntityUtils.toString(((org.apache.http.HttpResponse)
                // response).getEntity());
                methodResult = EntityUtils.toString(response.getEntity());
            }
            client.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            logger.debug("接口请求异常!");
        } catch (IOException e) {
            e.printStackTrace();
            logger.debug("接口请求异常!");
        }
        return methodResult;
    }

}
